<?php

namespace App\Http\Controllers\Chanjet;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class TplusInventoryLedger extends BaseController
{
    public function getExternalData(){
        $common = new TplusCommonController();
        $res = $common->getExternalData("TplusInventoryLedger",$this->mangerinfo['org_id']);
        return $res;
    }
    public function getlist(Request $request)
    {

        //需要报表缓存  单据日期查询  总计需要测试
        $memberinfo = $this->mangerinfo;
        $isgetall = $request->isgetall ?? 0;

        $start_time = isTimestamp($request->start_time)?date('Y-m-d',$request->start_time):$request->start_time;
        $end_time = isTimestamp($request->end_time)?date('Y-m-d',$request->end_time):$request->end_time;
        if(!$start_time || !$end_time) {
            return response()->json(['code'=>'001','msg'=>'查询条件不能为空']);
        }
        $dic[] = ['VoucherDate',$start_time,$end_time];
        $dic[] = ['cpid',$memberinfo['cpid']];

        $checkData = [
            'controllerName'=>'TplusInventoryLedger',
            'org_id'=>$this->mangerinfo['org_id'],
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData);
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }

        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusInventoryLedger::getList($request, $memberinfo, $isgetall);
        $totalField = $this->fieldTotal($list['list'],
            ['open_balance_num','open_balance_amount','current_revenue_num','current_revenue_amount','current_emit_num','current_emit_amount','balance_num','balance_amount']
            ,['warehouse_name','warehouse_no']);
        $list['total'] = $totalField;
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
}
